package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxm.servlet.BaseServlet;
import com.xm.dao.ProductDaoImpl;
import com.xm.dao.ProtypeDaoImpl;
import com.xm.entity.Banners;
import com.xm.entity.Product;
import com.xm.entity.producttype;
import com.xm.uti.DBManager;
import com.xm.uti.QueryDataResult;
import com.xm.uti.Result;

/**
 * 产品表后台方法调用 Servlet implementation class ProductServletAdmin
 */
@WebServlet("/admin/ProductServletAdmin")
public class ProductServletAdmin extends BaseServlet {
	ProductDaoImpl proDao = new ProductDaoImpl();
	ProtypeDaoImpl protypDao = new ProtypeDaoImpl();

	/**
	 * 分页查询、产品表和产品系列表的链接方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String name = req.getParameter("name");
		String Protype = req.getParameter("protype");
		String minPrice = req.getParameter("minPrice");
		String maxPrice = req.getParameter("maxPrice");
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		
		ArrayList<Product> List = proDao.getProFen(name, Protype, minPrice, maxPrice, page, limit);
		
		ArrayList<Product> List2 = proDao.getProFen(name, Protype, minPrice, maxPrice, null, null);

		
		String res = Result.toClient(0, "产品列表", List, List2.size());

		
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	/**
	 * 查产品类型
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProtypList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、调用dao层方法获取数据，
		ArrayList<producttype> ptotyList = protypDao.getAllProtype();
		// 将数据封装为layui需要的格式
		String res = Result.toClient(0, "产品系列表", ptotyList, ptotyList.size());

		// 2、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	/**
	 * 添加方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insertProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、从页面上拿到数据
		String Proname = req.getParameter("proname");// 表单中标签name的值
		String Protype = req.getParameter("protype");
		String color = req.getParameter("color");
		String market = req.getParameter("market");
		String Price = req.getParameter("price");
		String Brand = req.getParameter("brand");
		String Style = req.getParameter("style");
		String Material = req.getParameter("material");
		String Zuzhuang = req.getParameter("zuzhuang");
		String Dingzhi = req.getParameter("dingzhi");
		String Picurl = req.getParameter("picurl");
		String isShow = req.getParameter("isShow");
		String model = req.getParameter("model");
		String element = req.getParameter("element");
		String positioning = req.getParameter("positioning");
		String typename = req.getParameter("typename");

		// 2、调用dao层方法，将数据添加到数据库中
		Product product = new Product(0, Proname, Integer.valueOf(Protype), color, Double.valueOf(market),
				Double.valueOf(Price), Brand, Style, Material, Zuzhuang, Dingzhi, Picurl, isShow, model, element,
				positioning, typename);
		boolean res = proDao.insertPro(product);// 方法

		
		PrintWriter out = resp.getWriter();

		if (res) {
			out.write(Result.toClient(0, "添加成功"));
		} else {
			out.write(Result.toClient(1, "添加失败"));
		}
		
		out.flush();
		out.close();
	}

	/**
	 * 修改家具信息
	 */
	public void updatePro(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String Proname = req.getParameter("proname");
		String Protype = req.getParameter("protype");
		String color = req.getParameter("color");
		String market = req.getParameter("market");
		String Price = req.getParameter("price");
		String Brand = req.getParameter("brand");
		String Style = req.getParameter("style");
		String Material = req.getParameter("material");
		String Zuzhuang = req.getParameter("zuzhuang");
		String Dingzhi = req.getParameter("dingzhi");
		String Picurl = req.getParameter("picurl");
		String isShow = req.getParameter("isShow");
		String model = req.getParameter("model");
		String element = req.getParameter("element");
		String positioning = req.getParameter("positioning");
		String typename = req.getParameter("typename");
		String id = req.getParameter("id");

		 
		boolean res = proDao.updatePro(new Product(Integer.valueOf(id), Proname, Integer.valueOf(Protype), color,
				Double.valueOf(market), Double.valueOf(Price), Brand, Style, Material, Zuzhuang, Dingzhi, Picurl,
				isShow, model, element, positioning, typename));

		System.out.println("要修改的产品对象是" + res);
		
        String resJson="";
        
		if (res) {
			resJson=Result.toClient(0, "修改成功");
		} else {
			resJson=Result.toClient(1, "修改失败");
		}
		PrintWriter out = resp.getWriter();
		out.write(resJson);
		
		out.flush();
		out.close();
	}

	/**
	 * 删除方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取客户端传过来的参数
		String id = req.getParameter("id");
		if (id != null && !id.equals("")) {
			int id2 = Integer.valueOf(id);// 将字符串转换为整数类型
			// 2.调用dao层的方法，删除
			boolean res = proDao.delProduct(id2);
			// 3、讲数据返回到页面上
			PrintWriter out = resp.getWriter();

			if (res) {
				out.write(Result.toClient(0, "删除成功"));
			} else {
				out.write(Result.toClient(1, "删除失败"));
			}
			// 将数据封装为layui需要的格式
			out.flush();
			out.close();
		}
	}

}
